AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")

include("shared.lua")

function ENT:Initialize()
	self:SetModel("models/venompapa/wirecdlock.mdl")
	self:PhysicsInit(SOLID_VPHYSICS)
	self:SetMoveType(MOVETYPE_VPHYSICS)
	self:SetSolid(SOLID_VPHYSICS)
	
	local phys = self:GetPhysicsObject()
	if(phys:IsValid()) then
		phys:Wake()
	end
end

function ENT:OnRemove()
	if(ValidEntity(self:GetNetworkedEntity("owner"))) then
		ply = self:GetNetworkedEntity("owner");
		ply:SetNetworkedEntity("robot_core", nil);
	end
end

function ENT:OnTakeDamage(dmg)
	local amount = dmg:GetDamage();
	
	self:Damage(amount);
end

function ENT:PhysicsCollide(data, physobj)
	if(data.Speed > 250) then
		self:Damage(data.Speed * 0.01);
	end
end

function ENT:Use(activator, caller)
	return
end

function ENT:Think()
	local weight = 0;
	local constraints = constraint.GetAllConstrainedEntities(self);
	for _,ent in pairs(constraints) do
		local phys = ent:GetPhysicsObject();
		if(phys && phys:IsValid()) then
			weight = weight + phys:GetMass();
		end
	end
	self:SetNetworkedInt("weight", weight);
end

function ENT:Damage(amount)
	self:SetNetworkedInt("health", self:GetNetworkedInt("health") - amount);
	
	local health = self:GetNetworkedInt("health");
	
	if(health <= 50 && !self.IsSmoking) then
		self.IsSmoking = true;
		local effectData = EffectData();
		effectData:SetEntity(self);
		effectData:SetScale(0.5);
		//util.Effect("SMOKE", effectData);
	end
	
	if(health <= 0) then
		local effectData = EffectData();
		effectData:SetStart(self:GetPos());
		effectData:SetOrigin(self:GetPos());
		effectData:SetScale(1);
		util.Effect("Explosion", effectData);
		self:Remove();
	end
end